package com.ruibang.glass.portal.components;

import com.ruibang.glass.portal.constant.Constants;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class SsoToken {

	@Autowired
	private RedisSingleService redisService;


//	@Autowired
//	private RedisSingleService redisService;

	/**
	 * 生成token
	 * @param str
	 * @return
	 */
	public String generateToken(String str){
		String ticket = getToken();
		redisService.set(ticket,str,Constants.EXPIRE);
		return ticket;
	}
	
	/**
	 * 生成token
	 * @param str
	 * @param id
	 * @return
	 */
	public String generateSsoToken(String str,String id){
		String ticket = generateToken(str);
		Boolean flag = redisService.hasKey(Constants.T_SSO_TOKEN_USER+id);
		String t = ticket;
		if(flag){
			t = String.valueOf(redisService.get(Constants.T_SSO_TOKEN_USER+id));
			t = t+","+ticket;
		}
		redisService.set(Constants.T_SSO_TOKEN_USER+id,t, Constants.EXPIRE);
		return ticket;
	}
	
	/**
	 * 根据token获取信息
	 * @param token
	 * @return
	 */
	public String getByToken(String token){
		if(redisService.hasKey(token)){
			return redisService.get(token).toString();
		}else{
			return "";
		}
	}
	
	/**
	 * 删除token
	 * @param token
	 */
	public void deleteToken(String token){
		redisService.del(token);
	}
	
	private static String getToken(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
}
